/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;

import play.mvc.Controller;
import play.mvc.With;

import com.google.gson.*;
import java.util.*;
import models.*;

/**
 *
 * @author Ghadeer
 */
@With(Secure.class)
public class QuickSearchRendering extends Controller {

    /**
     *takes searchword and no.page and filter and returns topics and ideas resulting from the search
     *@param search 
     *the word to search with
     *@param page
     *the page displayed
     *@param sort 
     * the sorting used 
     */
    public static void searchOnClickSearchButton(String search, Integer page, String sort) {
        String userName = Security.connected();
        IdeaDeveloper id = (IdeaDeveloper) IdeaDeveloper.find("username = ?", userName).fetch().get(0);
        if (page == null) {
            page = 1;
        }

        QuickSearchResultsTopic resultsTopic = null;
        QuickSearchResultsIdea resultsIdea = null;
        QuickSearchResultsIdeaDeveloper resultsMember = null;
        QuickSearchResultsOrganization resultsOrganization = null;
        QuickSearchResultsEntity resultsEntity = null;
        QuickSearchResultsActionplan resultsActionplan = null;
        QuickSearchResultsTag resultsTag = null;

        resultsTag = QuickSearch.quickSearchTag(search, page, id, 3);
        resultsTopic = QuickSearch.searchTitleorDescriptionTopic(search, page, id, 3);
        resultsIdea = QuickSearch.searchDescriptionIdea(search, page, id, 3);
        resultsMember = QuickSearch.searchMemberSystem(search, page, id);
        resultsOrganization = QuickSearch.searchNameOrganization(search, page, id, 3);
        resultsEntity = QuickSearch.searchNameEntity(search, page, id, 3);
        resultsActionplan = QuickSearch.searchNameActionplan(search, page, id, 3);
        render("QuickSearchRendering/QuickSearchResultsTabs.html", resultsTag, resultsTopic, resultsIdea, resultsOrganization,
                resultsEntity, resultsActionplan, resultsMember, page, search);
    }

    /**
     * render JSON object with the tag results returned from the database
     * @param search
     * the word to search with
     * @param page
     * the displayed page
     * @param sort 
     * the sorting chosen
     */
    public static void searchAjaxTag(String search, String page, String sort) {
        String userName = Security.connected();
        IdeaDeveloper id = (IdeaDeveloper) IdeaDeveloper.find("username = ?", userName).fetch().get(0);
        QuickSearchResultsTag results = null;

        if (sort.equals("date")) {
            results = QuickSearch.quickSearchTag(search, Integer.parseInt(page), id, 1);
        }
        if (sort.equals("relevance")) {
            results = QuickSearch.quickSearchTag(search, Integer.parseInt(page), id, 2);
        }
        if (sort.equals("relatedItems")) {
            results = QuickSearch.quickSearchTag(search, Integer.parseInt(page), id, 3);
        }

        if (sort.equals("dateasc")) {
            results = QuickSearch.quickSearchTag(search, Integer.parseInt(page), id, 4);
        }
        if (sort.equals("relatedItemsasc")) {
            results = QuickSearch.quickSearchTag(search, Integer.parseInt(page), id, 5);
        }

        if (results.items.size() > 0) {

            String s = "" + results.items.get(0).name;
            for (int i = 1; i < results.items.size(); i++) {
                s = s + "&" + results.items.get(i).name;
            }
            JsonObject json = new JsonObject();
            json.addProperty("search", search);
            json.addProperty("page", Integer.parseInt(page));
            json.addProperty("countTag", results.count);
            json.addProperty("resultsTag", s);


            renderJSON(json.toString());
        }
    }

    /**
     * render JSON object with the topic results returned from the database
     * @param search
     * the word to search with
     * @param page
     * the displayed page
     * @param sort 
     * the sorting chosen
     */
    public static void searchAjaxTopic(String search, String page, String sort) {
        String userName = Security.connected();
        IdeaDeveloper id = (IdeaDeveloper) IdeaDeveloper.find("username = ?", userName).fetch().get(0);
        QuickSearchResultsTopic results = null;

        if (sort.equals("views")) {
            results = QuickSearch.searchTitleorDescriptionTopic(search, Integer.parseInt(page), id, 1);
        }
        if (sort.equals("relevance")) {
            results = QuickSearch.searchTitleorDescriptionTopic(search, Integer.parseInt(page), id, 2);
        }
        if (sort.equals("date")) {
            results = QuickSearch.searchTitleorDescriptionTopic(search, Integer.parseInt(page), id, 3);
        }
        if (sort.equals("rate")) {
            results = QuickSearch.searchTitleorDescriptionTopic(search, Integer.parseInt(page), id, 4);
        }

        if (sort.equals("viewsasc")) {
            results = QuickSearch.searchTitleorDescriptionTopic(search, Integer.parseInt(page), id, 5);
        }
        if (sort.equals("dateasc")) {
            results = QuickSearch.searchTitleorDescriptionTopic(search, Integer.parseInt(page), id, 6);
        }
        if (sort.equals("rateasc")) {
            results = QuickSearch.searchTitleorDescriptionTopic(search, Integer.parseInt(page), id, 7);
        }


        if (results.items.size() > 0) {
            String s1 = "" + results.items.get(0).id;
            for (int i = 0; i < results.items.size(); i++) {
                s1 = s1 + "&" + results.items.get(i).id;
            }
            String s = "" + results.items.get(0).title;
            for (int i = 1; i < results.items.size(); i++) {
                s = s + "&" + results.items.get(i).title;
            }
            JsonObject json = new JsonObject();
            json.addProperty("search", search);
            json.addProperty("pageTopic", Integer.parseInt(page));
            json.addProperty("countTopic", results.count);
            json.addProperty("resultsTopic", s);
            json.addProperty("idsTopic", s1);


            renderJSON(json.toString());
        }
    }

    /**
     * render JSON object with the ideas results returned from the database
     * @param search
     * the word to search with
     * @param page
     * the displayed page
     * @param sort 
     * the sorting chosen
     */
    public static void searchAjaxIdea(String search, String page, String sort) {
        String userName = Security.connected();
        IdeaDeveloper id = (IdeaDeveloper) IdeaDeveloper.find("username = ?", userName).fetch().get(0);
        QuickSearchResultsIdea results = null;

        if (sort.equals("views")) {
            results = QuickSearch.searchDescriptionIdea(search, Integer.parseInt(page), id, 1);
        }
        if (sort.equals("relevance")) {
            results = QuickSearch.searchDescriptionIdea(search, Integer.parseInt(page), id, 2);
        }
        if (sort.equals("date")) {
            results = QuickSearch.searchDescriptionIdea(search, Integer.parseInt(page), id, 3);
        }
        if (sort.equals("rate")) {
            results = QuickSearch.searchDescriptionIdea(search, Integer.parseInt(page), id, 4);
        }
        if (sort.equals("vote")) {
            results = QuickSearch.searchDescriptionIdea(search, Integer.parseInt(page), id, 5);
        }
        if (sort.equals("viewsasc")) {
            results = QuickSearch.searchDescriptionIdea(search, Integer.parseInt(page), id, 6);
        }
        if (sort.equals("dateasc")) {
            results = QuickSearch.searchDescriptionIdea(search, Integer.parseInt(page), id, 7);
        }
        if (sort.equals("rateasc")) {
            results = QuickSearch.searchDescriptionIdea(search, Integer.parseInt(page), id, 8);
        }
        if (sort.equals("voteasc")) {
            results = QuickSearch.searchDescriptionIdea(search, Integer.parseInt(page), id, 9);
        }
        if (results.items.size() > 0) {
            String s1 = "" + results.items.get(0).id;
            for (int i = 0; i < results.items.size(); i++) {
                s1 = s1 + "&" + results.items.get(i).id;
            }
            String s = "" + results.items.get(0).description;
            for (int i = 1; i < results.items.size(); i++) {
                s = s + "&" + results.items.get(i).description;
            }
            JsonObject json = new JsonObject();
            json.addProperty("search", search);
            json.addProperty("pageIdea", Integer.parseInt(page));
            json.addProperty("countIdea", results.count);
            json.addProperty("resultsIdea", s);
            json.addProperty("idsIdea", s1);

            renderJSON(json.toString());
        }
    }

    /**
     * render JSON object with the idea developers results returned from the database
     * @param search
     * the word to search with
     * @param page 
     * the displayed page
     */
    public static void searchAjaxMember(String search, String page) {
        QuickSearchResultsIdeaDeveloper results = null;
        String userName = Security.connected();
        IdeaDeveloper id = (IdeaDeveloper) IdeaDeveloper.find("username = ?", userName).fetch().get(0);
        results = QuickSearch.searchMemberSystem(search, Integer.parseInt(page), id);
        if (results.items.size() > 0) {
            String s1 = "" + results.items.get(0).id;
            for (int i = 0; i < results.items.size(); i++) {
                s1 = s1 + "&" + results.items.get(i).id;
            }
            String s = "" + results.items.get(0).username;
            for (int i = 1; i < results.items.size(); i++) {
                s = s + "&" + results.items.get(i).username;
            }
            JsonObject json = new JsonObject();
            json.addProperty("search", search);
            json.addProperty("page", Integer.parseInt(page));
            json.addProperty("countMember", results.count);
            json.addProperty("resultsMember", s);
            json.addProperty("idsMember", s1);

            renderJSON(json.toString());
        }
    }

    /**
     * render JSON object with tags with name contains the search parameter to be used in the autoCompleter
     * @param search 
     * the word to search with
     */
    public static void autoComplete(String search) {
        List<Tag> results;
        results = QuickSearch.getTags(search);

        JsonObject json = new JsonObject();
        String data = results.get(0).name;
        for (int i = 1; i < results.size(); i++) {
            data += "&" + results.get(i).name;
        }

        json.addProperty("tags", data);
        renderJSON(json.toString());
    }

    /**
     * render JSON object with idea developers with username contains the search parameter to be used in the autoCompleter
     * @param search 
     * the word to search with
     */
    public static void autoComplete1(String search) {
        List<IdeaDeveloper> results;
        results = QuickSearch.searchMemberSystemNoPaging(search);

        JsonObject json = new JsonObject();
        String data = results.get(0).username;
        for (int i = 1; i < results.size(); i++) {
            data += "&" + results.get(i).username;
        }

        json.addProperty("tags", data);
        renderJSON(json.toString());
    }

    /**
     * render JSON object with the organizations results returned from the database
     * @param search
     * the word to search with
     * @param page 
     * the displayed page
     */
    public static void searchAjaxOrganization(String search, String page, String sort) {
        String userName = Security.connected();
        IdeaDeveloper id = (IdeaDeveloper) IdeaDeveloper.find("username = ?", userName).fetch().get(0);
        QuickSearchResultsOrganization results = null;
        if (sort.equals("views")) {
            results = QuickSearch.searchNameOrganization(search, Integer.parseInt(page), id, 1);
        }
        if (sort.equals("relevance")) {
            results = QuickSearch.searchNameOrganization(search, Integer.parseInt(page), id, 2);
        }
        if (sort.equals("members")) {
            results = QuickSearch.searchNameOrganization(search, Integer.parseInt(page), id, 5);
        }
        if (sort.equals("date")) {
            results = QuickSearch.searchNameOrganization(search, Integer.parseInt(page), id, 3);
        }
        if (sort.equals("rate")) {
            results = QuickSearch.searchNameOrganization(search, Integer.parseInt(page), id, 4);
        }
        if (sort.equals("viewsasc")) {
            results = QuickSearch.searchNameOrganization(search, Integer.parseInt(page), id, 6);
        }
        if (sort.equals("membersasc")) {
            results = QuickSearch.searchNameOrganization(search, Integer.parseInt(page), id, 9);
        }
        if (sort.equals("dateasc")) {
            results = QuickSearch.searchNameOrganization(search, Integer.parseInt(page), id, 7);
        }
        if (sort.equals("rateasc")) {
            results = QuickSearch.searchNameOrganization(search, Integer.parseInt(page), id, 8);
        }

        if (results.items.size() > 0) {
            String s1 = "" + results.items.get(0).id;
            for (int i = 0; i < results.items.size(); i++) {
                s1 = s1 + "&" + results.items.get(i).id;
            }
            String s = "" + results.items.get(0).name;
            for (int i = 1; i < results.items.size(); i++) {
                s = s + "&" + results.items.get(i).name;
            }
            JsonObject json = new JsonObject();
            json.addProperty("search", search);
            json.addProperty("page", Integer.parseInt(page));
            json.addProperty("countOrganization", results.count);
            json.addProperty("resultsOrganization", s);
            json.addProperty("idsOrganization", s1);

            renderJSON(json.toString());
        }
    }

    /**
     * render JSON object with the entities results returned from the database
     * @param search
     * the word to search with
     * @param page 
     * the displayed page
     */
    public static void searchAjaxEntity(String search, String page, String sort) {
        String userName = Security.connected();
        IdeaDeveloper id = (IdeaDeveloper) IdeaDeveloper.find("username = ?", userName).fetch().get(0);
        QuickSearchResultsEntity results = null;
        if (sort.equals("views")) {
            results = QuickSearch.searchNameEntity(search, Integer.parseInt(page), id, 1);
        }
        if (sort.equals("relevance")) {
            results = QuickSearch.searchNameEntity(search, Integer.parseInt(page), id, 2);
        }
        if (sort.equals("date")) {
            results = QuickSearch.searchNameEntity(search, Integer.parseInt(page), id, 3);
        }
        if (sort.equals("rate")) {
            results = QuickSearch.searchNameEntity(search, Integer.parseInt(page), id, 4);
        }
        if (sort.equals("viewsasc")) {
            results = QuickSearch.searchNameEntity(search, Integer.parseInt(page), id, 5);
        }
        if (sort.equals("dateasc")) {
            results = QuickSearch.searchNameEntity(search, Integer.parseInt(page), id, 6);
        }
        if (sort.equals("rateasc")) {
            results = QuickSearch.searchNameEntity(search, Integer.parseInt(page), id, 7);
        }
        if (results.items.size() > 0) {
            String s1 = "" + results.items.get(0).id;
            for (int i = 0; i < results.items.size(); i++) {
                s1 = s1 + "&" + results.items.get(i).id;
            }
            String s = "" + results.items.get(0).name;
            for (int i = 1; i < results.items.size(); i++) {
                s = s + "&" + results.items.get(i).name;
            }
            JsonObject json = new JsonObject();
            json.addProperty("search", search);
            json.addProperty("page", Integer.parseInt(page));
            json.addProperty("countEntity", results.count);
            json.addProperty("resultsEntity", s);
            json.addProperty("idsEntity", s1);

            renderJSON(json.toString());
        }
    }

    /**
     *takes searchword and no.page and filter and returns members resulting from the search
     *@param search
     *the word to search with
     *@param page
     *the displayed page
     *@param sort
     *the sorting chosen
     *renders JSON object with results
     */
    public static void searchAjaxActionplan(String search, String page, String sort) {
        String userName = Security.connected();
        IdeaDeveloper id = (IdeaDeveloper) IdeaDeveloper.find("username = ?", userName).fetch().get(0);
        QuickSearchResultsActionplan results = null;
        if (sort.equals("views")) {
            results = QuickSearch.searchNameActionplan(search, Integer.parseInt(page), id, 1);
        }
        if (sort.equals("relevance")) {
            results = QuickSearch.searchNameActionplan(search, Integer.parseInt(page), id, 2);
        }
        if (sort.equals("date")) {
            results = QuickSearch.searchNameActionplan(search, Integer.parseInt(page), id, 3);
        }
        if (sort.equals("rate")) {
            results = QuickSearch.searchNameActionplan(search, Integer.parseInt(page), id, 4);
        }
        if (sort.equals("viewsasc")) {
            results = QuickSearch.searchNameActionplan(search, Integer.parseInt(page), id, 5);
        }
        if (sort.equals("dateasc")) {
            results = QuickSearch.searchNameActionplan(search, Integer.parseInt(page), id, 6);
        }
        if (sort.equals("rateasc")) {
            results = QuickSearch.searchNameActionplan(search, Integer.parseInt(page), id, 7);
        }
        if (results.items.size() > 0) {
            String s1 = "" + results.items.get(0).id;
            for (int i = 1; i < results.items.size(); i++) {
                s1 = s1 + "&" + results.items.get(i).id;
            }
            String s = "" + results.items.get(0).name;
            for (int i = 1; i < results.items.size(); i++) {
                s = s + "&" + results.items.get(i).name;
            }
            JsonObject json = new JsonObject();
            json.addProperty("search", search);
            json.addProperty("page", Integer.parseInt(page));
            json.addProperty("countActionplan", results.count);
            json.addProperty("resultsActionplan", s);
            json.addProperty("idsActionplan", s1);

            renderJSON(json.toString());
        }
    }
}
